package com.homegoods.classify.ui.fragment;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.classify.R;
import com.homegoods.classify.databinding.ClassifyFragmentBrandListBinding;
import com.homegoods.classify.model.TestBean.DatasBean.AddressListBean;
import com.homegoods.classify.persenter.BrandPresenter;
import com.homegoods.aroutercomment.ARouterPath;
import com.kedll.library.adapter.MyExpandableListAdapter;
import com.kedll.library.fragment.KBaseFragment;
import com.kedll.library.util.ViewUtil;
import com.kedll.library.widget.LetterListView;

/**
 * @author QiQi
 * @descrip 品牌列表
 */
@Route(path = ARouterPath.PATH_CLASSIFY)
public class BrandListFragment extends KBaseFragment<ClassifyFragmentBrandListBinding, BrandPresenter> {

    private TextView mTipText;
    private View mTipView;
    private LetterListView mLetterList;
    private ExpandableListView mExpListView;
    private String[] sps = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"};
    private MyExpandableListAdapter<AddressListBean> brandAdapter;

    @Override
    protected void init() {
        super.init();
        mExpListView = mBinding.list;
        mLetterList = mBinding.letterList;
        mTipText = mBinding.includeTip.tips;
        mTipView = mBinding.includeTip.tipLayout;
        brandAdapter = mPresenter.getAdapter();
        mExpListView.setAdapter(brandAdapter);
        mLetterList.setLetterLabel(sps);

        brandAdapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                int count = brandAdapter.getGroupCount();
                for (int j = 0; j < count; j++) {
                    mExpListView.expandGroup(j);
                }
            }
        });

        mPresenter.loadData();
    }

    @Override
    protected void initEvent(Bundle savedInstanceState) {
        mExpListView.setOnGroupClickListener((parent, v, groupPosition, id) -> true);

        mExpListView.setOnChildClickListener((parent, v, groupPosition, childPosition, id) -> {
            String child = brandAdapter.getChild(groupPosition, childPosition).getName();
            String sid = brandAdapter.getChild(groupPosition, childPosition).getId();
            ARouter.getInstance()
                    .build(ARouterPath.PATH_SEARCH_BRAND)
                    .withString("searchContent", child)
                    .withString("sid", sid)
                    .navigation();
            return true;
        });

        mLetterList.setOnTouchingLetterChangedListener(new LetterListView.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterEnd() {
                if (mTipView.isShown()) {
                    mTipView.setVisibility(View.GONE);
                }
            }

            @Override
            public void onTouchingLetterChanged(String s, float y, float x) {
                if (brandAdapter == null) {
                    return;
                }
                if ("#".equals(s)) {
                    mExpListView.setSelection(brandAdapter.getGroupCount() - 1);
                } else {
                    int index = brandAdapter.indexOf(s);
                    if (index != -1) {
                        mExpListView.setSelectedGroup(index);
                        Log.i(TAG, "selector:" + s);
                    }
                }
                showTip(s);
            }
        });
    }

    @Override
    protected BrandPresenter getPresenter() {
        return new BrandPresenter(getContext(), null);
    }

    @Override
    protected int getLayout() {
        return R.layout.classify_fragment_brand_list;
    }

    private void showTip(String tip) {

        ViewUtil.setGone(mTipView, false);
        mTipText.setText(tip);

    }

}
